<template>
	<view>
		<!-- 使用自定义的搜索组件 -->
		<view class="search">
			<search-bar @click="goSearch"></search-bar>
		</view>
		<!-- 轮播图 -->
		<swiper indicator-active-color="#fff" circular :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
			<swiper-item v-for="(item, i) in swiperList" :key="i">
			    <navigator class="swiper-item" :url="'/subpkg/goods-detail/goods-detail?goods_id=' + item.goods_id">
			      <!-- 动态绑定图片的 src 属性 -->
			      <image :src="item.image_src"></image>
			    </navigator>
			</swiper-item>
		</swiper>
		<!-- 分类导航区域 -->
		<view class="nav-list">
		   <view class="nav-item" v-for="(item, index) in navList" :key="index">
				 <!-- getCate() 点击第一项，切换到分类页面 -->
		     <image :src="item.image_src" @click="getCate(index)"></image>
		   </view>
		</view>
		<!-- 楼层区域 -->
		<view class="floor-list">
			<view class="floor-item" v-for="(item, index) in floorList" :key="index">
				<!-- 标题 -->
				<view class="floor-title">
					<image :src="item.floor_title.image_src" mode=""></image>
				</view>
				<!-- 内容 -->
				<view class="floor-content">
					<!-- 左 -->
					<view class="left">
						<image @click="goList(item.product_list[0])" :style="{width: item.product_list[0].image_width + 'rpx'}" :src="item.product_list[0].image_src" mode="widthFix"></image>
					</view>
					<!-- 右 -->
					<view class="right">
						<image @click="goList(e)" :style="{width: e.image_width + 'rpx'}" v-if="i !== 0" v-for="e, i in item.product_list" :key="i" :src="e.image_src" mode="widthFix"></image>
					</view>
					
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import tabBarMixins from '../../mixins/tabBarMixins.js'
	export default {
		mixins: [tabBarMixins],
		data() {
			return {
				swiperList: [] ,// 轮播图的数据列表
				navList: [], // 分类导航的数据
				floorList: [] //楼层数据
				
			}
		},
		// 页面一加载时，请求数据
		onLoad() {
			this.getSwiperList()
			this.getNavList()
			this.getFloorList()
		},
		methods: {
			// 跳转到搜索页面
			goSearch() {
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			},
			// 跳转到商品列表
			goList(obj) {
				// console.log(obj)
				const params = obj.navigator_url.split('?')[1]
				uni.navigateTo({
					url: `/subpkg/goods-list/goods-list?${params}`
				})
			},
			// 获取楼层数据
			async getFloorList() {
				const {data: {message, meta}} = await uni.$http.get('/api/public/v1/home/floordata')
				if(meta.status !== 200) return uni.$showMsg()
				this.floorList = message
			},
			// 跳转到分类页面
			getCate(index) {
				if(index === 0){
					// 跳转到tabBar页面
					uni.switchTab({
						url: '/pages/cate/cate'
					})
				}
			},
			// 获取分类导航的数据
			async getNavList() {
				const {data: {message, meta}} = await uni.$http.get('/api/public/v1/home/catitems')
				// 判断请求是否成功
				if(meta.status !== 200) return uni.$showMsg('获取导航数据失败')
				this.navList = message
			},
			// 获取轮播图数据
			async getSwiperList() {
				const {data:{message, meta}} = await uni.$http.get('/api/public/v1/home/swiperdata')
				// 判断请求是否成功
				if(meta.status !== 200) return uni.$showMsg()
				this.swiperList = message
			}
		}
	}
</script>

<style lang="scss">
	// 搜索框
	.search {
		position: sticky;
		top: 0;
		left: 0;
		z-index: 999;
	}
	// 楼层
	.floor-title {
	  image {
			height: 60rpx;
			width: 100%;
		}
	}
	.floor-content {
		display: flex;
		padding: 6px;
		.right {
			margin-left: 6px;
			display: flex;
			justify-content: space-between;
			flex-wrap: wrap;
		}
	}
	
	// 分类
.nav-list {
	display: flex;
	justify-content: space-around;
	margin: 15px 0;
		.nav-item, image {
			width: 128rpx;
			height: 140rpx;
		}
}

// 轮播图
swiper {
 height: 330rpx;

 .swiper-item, image {
   width: 100%;
   height: 100%;
 }
}
</style>
